"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Compatibility
Case Name   : 表含自增字段，指定ignore参数
Create At   : 2023/06/02
Owner       : n@ningyali
Description :
        1.建表
        2.导入数据，自增字段整数递增
        3.导入数据，自增字段整数递减
        4.导入数据，自增字段乱序
        5.导入数据，自增字段为浮点型数据
        6.导入重复数据，指定参数ignore
        7.清理环境
Expect      :
        1.创建成功
        2.成功
        3.成功
        4.成功
        5.成功
        6.成功
        7.成功
History     :
"""
import os
import unittest

from yat.test import Node

from testcase.COMPATIBILITY.GRAMMAR.LOAD. \
    Opengauss_Compatibility_Grammar_Load_Common import LoadData
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class LoadData0029(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.load_data = LoadData()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.case_lable = 'load_data_0029'
        self.db_name = f'db_{self.case_lable}'
        self.t_name = f't_{self.case_lable}'
        self.file_path = os.path.join('/home', self.pri_user.ssh_user)
        self.cols = 'c_01 int primary key auto_increment,' \
                    'c_02 int,c_03 char(15),c_04 nvarchar2(15),' \
                    'c_05 numeric(10)'

        text1 = '--pre_step1：创建数据文件；expect:创建成功'
        self.log.info(text1)
        self.data1 = "(11,121,'vfadf','欣欣向荣',12.15343)" \
                     "(22,126,'jrytu','快乐୧(๑•̀◡•́๑)૭',1781.1518)" \
                     "(33,146,'vfadf','欣欣向荣',12.15343)"
        self.data2 = "(11,121,'vfadf','欣欣向荣',12.15343)" \
                     "(10,126,'jrytu','快乐୧(๑•̀◡•́๑)૭',1781.1518)" \
                     "(09,146,'vfadf','欣欣向荣',12.15343)"
        self.data3 = "(11,121,'vfadf','欣欣向荣',12.15343)" \
                     "(30,111,'jrytu','快乐୧(๑•̀◡•́๑)૭',1781.1518)" \
                     "(1,106,'vfadf','欣欣向荣',12.15343)"
        self.data4 = "(11.55,121,'vfadf','欣欣向荣',12.15343)" \
                     "(30.44,111,'jrytu','快乐୧(๑•̀◡•́๑)૭',1781.1518)" \
                     "(12.99,106,'vfadf','欣欣向荣',12.15343)"
        self.data5 = "(11,121,'vfadf','欣欣向荣',12.15343)" \
                     "(22,126,'jrytu','快乐୧(๑•̀◡•́๑)૭',1781.1518)" \
                     "(33,146,'vfadf','欣欣向荣',12.15343)" \
                     "(11,121,'vfadf','欣欣向荣',12.15343)" \
                     "(22,126,'jrytu','快乐୧(๑•̀◡•́๑)૭',1781.1518)" \
                     "(33,146,'vfadf','欣欣向荣',12.15343)" \
                     "(11,121,'vfadf','欣欣向荣',12.15343)" \
                     "(22,126,'jrytu','快乐୧(๑•̀◡•́๑)૭',1781.1518)" \
                     "(33,146,'vfadf','欣欣向荣',12.15343)"
        res = self.load_data.create_data_file(self.pri_user, self.case_lable,
                                              self.data1, self.data2,
                                              self.data3, self.data4,
                                              self.data5)
        self.assertTrue(res, '执行失败:' + text1)

        text2 = '--pre_step2：创建兼容B库；expect:创建成功'
        self.log.info(text2)
        res = self.load_data.create_b_db(self.pri_sh, self.db_name)
        self.assertTrue(res, '执行失败:' + text2)

    def test_load(self):
        text = '----step1：建表；expect：建表成功----'
        self.log.info(text)
        res = self.load_data.create_table(self.pri_sh, self.db_name,
                                          self.t_name, self.cols)
        self.assertTrue(res, '执行失败:' + text)

        text = '----step2：导入数据，自增字段整数递增；expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_1.txt')
        res1 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path,
                                        into_type='ignore', line_start='(',
                                        line_end=')')
        rows, _ = self.common.parse_sql_query_res(res1)
        expect = [['11', '121', 'vfadf', '欣欣向荣', '12'],
                  ['22', '126', 'jrytu', '快乐୧(๑•̀◡•́๑)૭', '1781'],
                  ['33', '146', 'vfadf', '欣欣向荣', '12']]
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '----step3：导入数据，自增字段整数递减；expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_2.txt')
        res2 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path,
                                        into_type='ignore', line_start='(',
                                        line_end=')')
        rows, _ = self.common.parse_sql_query_res(res2)
        expect = [['11', '121', 'vfadf', '欣欣向荣', '12'],
                  ['10', '126', 'jrytu', '快乐୧(๑•̀◡•́๑)૭', '1781'],
                  ['9', '146', 'vfadf', '欣欣向荣', '12']]
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '----step4：导入数据，自增字段乱序；expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_3.txt')
        res3 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path,
                                        into_type='ignore', line_start='(',
                                        line_end=')')
        rows, _ = self.common.parse_sql_query_res(res3)
        expect = [['11', '121', 'vfadf', '欣欣向荣', '12'],
                  ['30', '111', 'jrytu', '快乐୧(๑•̀◡•́๑)૭', '1781'],
                  ['1', '106', 'vfadf', '欣欣向荣', '12']]
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '----step5：导入数据，自增字段为浮点型；expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_4.txt')
        res4 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path,
                                        into_type='ignore', line_start='(',
                                        line_end=')')
        rows, _ = self.common.parse_sql_query_res(res4)
        expect = [['12', '121', 'vfadf', '欣欣向荣', '12'],
                  ['30', '111', 'jrytu', '快乐୧(๑•̀◡•́๑)૭', '1781'],
                  ['13', '106', 'vfadf', '欣欣向荣', '12']]
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '----step6：导入重复数据，指定参数ignore；expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_5.txt')
        res5 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path,
                                        into_type='ignore', line_start='(',
                                        line_end=')')
        rows, _ = self.common.parse_sql_query_res(res5)
        expect = [['11', '121', 'vfadf', '欣欣向荣', '12'],
                  ['22', '126', 'jrytu', '快乐୧(๑•̀◡•́๑)૭', '1781'],
                  ['33', '146', 'vfadf', '欣欣向荣', '12']]
        self.assertEqual(rows, expect, '执行失败' + text)

    def tearDown(self):
        self.log.info('----step7：清理环境；expect：清理成功----')
        res = self.load_data.clear_env(self.pri_sh, self.pri_user,
                                       self.db_name,
                                       os.path.join(self.file_path,
                                                    f"{self.case_lable}*"))
        self.assertTrue(res, '执行失败:清理环境')
        self.log.info(f'----{os.path.basename(__file__)} end----')
